package com.ym.controller.doctor.work;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.redis.RedisKeyEnum;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.entity.CounsellingRoomEntity;
import com.ym.medical.domain.entity.InquiryAnnexEntity;
import com.ym.medical.domain.entity.InquiryOrderContentEntity;
import com.ym.medical.domain.vo.GrabAndAnswerVo;
import com.ym.medical.domain.vo.InquiryOrderInfoVo;
import com.ym.medical.domain.vo.OrdersVo;
import com.ym.medical.enums.CounsellingRoomCourTypeEnum;
import com.ym.medical.service.IInquiryAnnexSV;
import com.ym.medical.service.IInquiryOrderContentSV;
import com.ym.medical.service.IInquiryOrderSV;
import com.ym.medical.service.IUserSV;
import com.ym.spring.SessionContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 订单操作相关
 *
 * @author: 李涛
 * @version: 2019年05月15日 10:15
 */
@Api(tags = "医生端-首页-订单相关")
@RestController
@ApiSign
@RequestMapping(value = "/doctor/work/order",headers = Constants.Api.VERSION)
@Validated
public class OrderController extends BaseController {

	@Autowired
	private IInquiryOrderSV inquiryOrderSV;

	@Autowired
	private IInquiryOrderContentSV inquiryOrderContentSV;

	@Autowired
	private IInquiryAnnexSV inquiryAnnexSV;

	@Autowired
	private IUserSV userSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("抢单医生刷单")
	@GetMapping("/pull")
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = OrdersVo.class),
	})

	public APIResponse<List<OrdersVo>> orders() throws Exception {
		RedisKeyEnum poolType = null;
		CounsellingRoomCourTypeEnum doctorType = SessionContext.getCurrDoctorType();
		if (doctorType == CounsellingRoomCourTypeEnum.GENERAL) {
			poolType = RedisKeyEnum.GENERAL_DOCTOR_POOL;
		} else if (doctorType == CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT) {
			poolType = RedisKeyEnum.SPECIALIST_DOCTOR_POOL;
		}
		List<OrdersVo> orders = inquiryOrderSV.getOrders(poolType, SessionContext.getCurrUser());
		return successData(orders);
	}

	@Log
	@ApiOperation(value = "医生抢单", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "抢单ID", required = true, paramType = "form"),
	})
	@PostMapping(value = "/grab", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse grabOrder(
			@NotEmpty String orderId
	) throws Exception {
		RedisKeyEnum poolType = null;
		CounsellingRoomCourTypeEnum doctorType = SessionContext.getCurrDoctorType();
		if (doctorType == CounsellingRoomCourTypeEnum.GENERAL) {
			poolType = RedisKeyEnum.GENERAL_DOCTOR_POOL;
		} else if (doctorType == CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT) {
			poolType = RedisKeyEnum.SPECIALIST_DOCTOR_POOL;
		}
		inquiryOrderSV.grabOrder(orderId, poolType,SessionContext.getCurrUser());
		return successMsg("抢单成功！");
	}

	@Log
	@ApiOperation(value = "医生抢单并答复", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "抢单ID", required = true, paramType = "form"),
	})
	@PostMapping(value = "/grabAnswer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<GrabAndAnswerVo> grabAnswer(
			@NotEmpty String orderId
	) throws Exception {
		RedisKeyEnum poolType = null;
		CounsellingRoomCourTypeEnum doctorType = SessionContext.getCurrDoctorType();
		if (doctorType == CounsellingRoomCourTypeEnum.GENERAL) {
			poolType = RedisKeyEnum.GENERAL_DOCTOR_POOL;
		} else if (doctorType == CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT) {
			poolType = RedisKeyEnum.SPECIALIST_DOCTOR_POOL;
		}
		GrabAndAnswerVo grabAndAnswerVo = inquiryOrderSV.grabOrderAnswer(orderId, poolType,SessionContext.getCurrUser());
		return successData(grabAndAnswerVo);
	}

	@Log
	@ApiOperation(value = "医生主动丢弃订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "抢单ID", required = true, paramType = "form"),
	})
	@PostMapping(value = "/fall", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse fallOrder(
			@NotEmpty String orderId
	) throws Exception {
		RedisKeyEnum poolType = null;
		CounsellingRoomCourTypeEnum doctorType = SessionContext.getCurrDoctorType();
		if (doctorType == CounsellingRoomCourTypeEnum.GENERAL) {
			poolType = RedisKeyEnum.GENERAL_DOCTOR_POOL;
		} else if (doctorType == CounsellingRoomCourTypeEnum.SPECIALIZED_SUBJECT) {
			poolType = RedisKeyEnum.SPECIALIST_DOCTOR_POOL;
		}
		inquiryOrderSV.fallOrder(orderId, poolType, SessionContext.getCurrUser());
		return success();
	}

	@Log
	@ApiOperation(value = "退诊订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", required = true, paramType = "form"),
	})
	@PostMapping(value = "/withdrawalOrder", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse withdrawalOrder(
			@NotNull Long orderId
	) throws Exception {
		inquiryOrderSV.withdrawalOrder(orderId);
		return success();
	}


	@Log(dataBase = false, printBody = false)
	@ApiOperation("获取订单详情信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", required = true, paramType = "query"),
	})
	@GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse<InquiryOrderInfoVo> orderInfo(
			@NotNull Long orderId
	) {
		InquiryOrderInfoVo inquiryOrderInfoVo = new InquiryOrderInfoVo();
		inquiryOrderInfoVo.setOrder(inquiryOrderSV.findById(orderId));
		inquiryOrderInfoVo.setAskContentExtr(inquiryOrderContentSV.selectList(
				TK.select(InquiryOrderContentEntity.class).orderByClause(" id asc ").where()
				.andEqualTo(InquiryOrderContentEntity::getOrderId,orderId)
				.end()
		));
		inquiryOrderInfoVo.setAttach(inquiryAnnexSV.selectList(InquiryAnnexEntity.builder().ioId(orderId).build()));
		return successData(inquiryOrderInfoVo);
	}

	@Log
	@ApiOperation("修改咨询状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "courStatus", value = "咨询状态", dataType = "String", required = true, paramType = "query"),
	})
	@PostMapping(value = "/upate/status", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse upodateStatus(@RequestParam CounsellingRoomEntity counsellingRoom) {
		boolean flag = inquiryOrderSV.updateStatus(counsellingRoom);
		if (flag) {
			return successMsg("修改咨询状态成功！");
		}
		return successMsg("修改咨询状态失败！");
	}

	@Log
	@ApiOperation("答复订单")
	@PostMapping(value = "/answer", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse answerOrder(Long orderId) {
		Long doctorId = ShiroUtil.getLoginId();
		CounsellingRoomCourTypeEnum doctorType = SessionContext.getCurrDoctorType();
		return successData(inquiryOrderSV.answerOrder(orderId, doctorId, doctorType));
	}

	@Log
	@ApiOperation("主动完成订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "String", required = true, paramType = "form"),
			@ApiImplicitParam(name = "roomId", value = "聊天室ID", dataType = "String", required = true, paramType = "form"),
	})
	@PostMapping(value = "/finish", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse finish(
			@NotNull Long orderId,
			@NotNull Long roomId
	) {
		inquiryOrderSV.autoCompleteOrder(orderId, roomId);
		return success();
	}

}
